package com.hst.capacity.facade.rest;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.hst.capacity.application.biz.IntercityOrderBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.intercity.*;
import com.hst.capacity.domain.model.request.price.IntercityPriceParam;
import com.hst.capacity.domain.model.request.price.IntercityRePriceParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.order.OrderPriceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  城际订单录入模块Controller，城际订单 CRUD，检索等。
 *
 * */
@Slf4j
@RequestMapping("/order/intercity")
@RestController
public class IntercityOrderController {

    @Autowired
    private IntercityOrderBiz icOrderControlBiz;

    /**
     * 城际订单保存
     * @param param
     * @return
     */
    @PostMapping("/save")
    private DefaultVO saveIntercityOrder(@RequestBody @Valid IntercityCreateOrderParam param){
        return success(icOrderControlBiz.saveIntercityOrder(param).getIdOrder());
    }

    /**
     * 创建城际订单
     * @param param
     * @return
     */
    @PostMapping("/create")
    private DefaultVO createIntercityOrder(@RequestBody @Valid IntercityCreateOrderParam param){
        if (Constants.ExSubBizType.StationToStation == param.getSubBizType()) {
            if (StrUtil.isEmpty(param.getIdStation()))
                throw new BusinessException(4001,"汽车站点ID[idStation]不能为空！");
        }
        return success(icOrderControlBiz.createIntercityOrder(param).getIdOrder());
    }

    /**
     * 更新订单
     * @param param
     * @return
     */
    @PostMapping("/update")
    private DefaultVO updateIntercityOrder(@RequestBody @Valid IntercityCreateOrderParam param){
        return success(icOrderControlBiz.updateIntercityOrder(param).getIdOrder());
    }

    /**
     * 根据城市获取线路列表
     * @param
     * @return
     */
    @PostMapping("/cityLines")
    private DefaultVO getCityLines(@RequestBody Map<String,String> param){
        String cityCode = param.get("cityCode");
        if (StrUtil.isEmpty(cityCode))
            throw new BusinessException(4001,"cityCode不能为空！");
        int subBizType = Integer.parseInt(param.get("subBizType"));
        if (Constants.IcSubBizType.CustomJourney == subBizType) {
            return success(icOrderControlBiz.getCustomCityLines(cityCode));
        }else{
            return success(icOrderControlBiz.getCityLines(cityCode));
        }
    }

    /**
     * 根据城际定制线路列表
     * @param
     * @return
     */
    @PostMapping("/custom/cityLines")
    private DefaultVO getCustomCityLines(@RequestBody Map<String,String> param){
        String cityCode = param.get("cityCode");
        if (StrUtil.isEmpty(cityCode))
            throw new BusinessException(4001,"cityCode不能为空！");
        return success(icOrderControlBiz.getCustomCityLines(cityCode));
    }

    /**
     * 根据城际定制站点列表
     * @param
     * @return
     */
    @PostMapping("/custom/stations")
    private DefaultVO getCustomStations(@RequestBody Map<String,String> param){
        String cityCode = param.get("cityCode");
        if (StrUtil.isEmpty(cityCode))
            throw new BusinessException(4001,"cityCode不能为空！");
        return success(icOrderControlBiz.getCityLines(cityCode));
    }

    /**
     * 根据运营商获取线路列表
     * @param
     * @return
     */
    @PostMapping("/lines/idf")
    private DefaultVO getIcLineByIdFranchisee(@RequestBody Map<String,String> param){
        String idFranchisee;
        if (StrUtil.isEmpty(idFranchisee=param.get("idFranchisee")))throw new BusinessException(4003,"运营商ID[idFranchisee]不能为空");
        return success(icOrderControlBiz.getIcLineByIdFranchisee(idFranchisee));
    }

    /**
     * 获取今日录单信息
     * @param param
     * @return
     */
    @PostMapping("/list/new")
    private DefaultVO getNewIcOrderList(@RequestBody @Valid IcListNewParam param){
        return success(icOrderControlBiz.getNewIcOrderList(param));
    }

    /**
     * 修改订单价格
     * @param param
     * @return
     */
    @PostMapping("/recalculate/price")
    private DefaultVO reCalculatePrice(@RequestBody @Valid IntercityRePriceParam param){
        return success(icOrderControlBiz.reCalculatePrice(param));
    }

    /**
     * 更换派遣司机
     * @param param
     * @return
     */
    @PostMapping("/redispatch/driver")
    private DefaultVO reDispatchDrive(@RequestBody @Valid DispatchDriverParam param){
        return success(icOrderControlBiz.reDispatchDriver(param));
    }

    /**
     * 站点驳车派单
     * @param param
     * @return
     */
    @PostMapping("/dispatch/driver")
    private DefaultVO DispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        if (StrUtil.isEmpty(param.getIdDriver()))
            throw new BusinessException(4001,"司机ID[idDriver]不能为空");
        return success(icOrderControlBiz.dispatchDriver(param));
    }

    /**
     * 取消站点驳车派单
     * @param param
     * @return
     */
    @PostMapping("/undispatch/driver")
    private DefaultVO stpUnDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(icOrderControlBiz.unDispatchDriver(param));
    }

    /**
     * 虚拟接单
     * @param param
     * @return
     */
    @PostMapping("/virtual/accept")
    private DefaultVO virtualDispatchDriver(@RequestBody @Valid DispatchParam param){
        // todo 虚拟接单
        return success(icOrderControlBiz.virtualAcceptOrder(param));
    }

    /**
     * 取消订单
     * @param json
     * @return
     */
    @PostMapping("/cancel/order")
    private DefaultVO cancelOrder(@RequestBody String json){
        JSON parse = JSONUtil.parse(json);
        String idOrder = StrUtil.toString(parse.getByPath("idOrder"));
        log.info("intercity cancelOrder:" + parse);
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"idOrder不能为空！");
        return success(icOrderControlBiz.cancelOrder(idOrder));
    }

    /**
     * 计算订单价格/里程/耗时
     * @param param
     * @return
     */
    @PostMapping("/price")
    private DefaultVO intercityPrice(@RequestBody @Valid IntercityPriceParam param){
        OrderPriceVO vo = new OrderPriceVO();
        if (Constants.ExSubBizType.InnerCity == param.getSubBizType()) {
            vo = icOrderControlBiz.getIntercityPrice(param);
        }
        if (Constants.ExSubBizType.StationToStation == param.getSubBizType()) {
            if (StrUtil.isEmpty(param.getIdStation()))
                throw new BusinessException(4001,"汽车站点ID[idStation]不能为空！");
            vo = icOrderControlBiz.getCommonPrice(param);
        }
        return success(vo);
    }

    /**
     * 订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO icOrderSearch(@RequestBody @Valid IcListParam param){
        boolean historyFlg = true;
        if (StrUtil.isEmpty(param.getHistoryKbn()))
            historyFlg = false;
        return success(icOrderControlBiz.icOrderSearch(param,historyFlg));
    }

    /**
     * 订单检索查询任务列表
     * @param param
     * @return
     */
    @PostMapping("/search/task")
    private DefaultVO icOrderSearchTask(@RequestBody @Valid IcListTaskParam param){
        return success(icOrderControlBiz.icOrderSearchTask(param));
    }

    /**
     * 任务详情
     * @param param
     * @return
     */
    @PostMapping("/task/detail")
    private DefaultVO icTaskDetail(@RequestBody Map<String,String> param){
        String idTask = param.get("idTask");
        if (StrUtil.isEmpty(idTask))
            throw new BusinessException(4001,"任务ID[idTask]不能为空");
        return success(icOrderControlBiz.icTaskDetail(idTask));
    }

    /**
     * 历史任务详情
     * @param param
     * @return
     */
    @PostMapping("/task/history/detail")
    private DefaultVO icTaskHistoryDetail(@RequestBody Map<String,String> param){
        String idTask = param.get("idTask");
        if (StrUtil.isEmpty(idTask))
            throw new BusinessException(4001,"任务ID[idTask]不能为空");
        return success(icOrderControlBiz.icTaskHistoryDetail(idTask));
    }

    /**
     * 订单详情
     * @param param
     * @return
     */
    @PostMapping("/detail")
    private DefaultVO icOrderDetail(@RequestBody @Valid Map<String,String> param){
        String idOrder;
        if (StrUtil.isEmpty(idOrder=param.get("idOrder")))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.icOrderDetail(idOrder,false));
    }

    /**
     * 订单销毁
     * @param param
     * @return
     */
    @PostMapping("/destroy")
    private DefaultVO icOrderDestroy(@RequestBody @Valid IcOrderDestroyParam param){
        return success(icOrderControlBiz.icOrderDestroy(param));
    }

    /**
     * 修改备注
     * @param param
     * @return
     */
    @PostMapping("/re/memo")
    private DefaultVO icReMemo(@RequestBody Map<String,String> param){
        String idOrder=param.get("idOrder");
        String memo=param.get("memo");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.icReMemo(idOrder,memo));
    }

    /**
     * 订单支付信息
     * @param param
     * @return
     */
    @PostMapping("/billInfo")
    private DefaultVO orderBillInfo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.getOrderBillInfo(idOrder,false));
    }

    @PostMapping("/list/settlement/view")
    private DefaultVO icOrderBillViewLst(@RequestBody @Valid IcSettlementViewListParam param){
        return success(icOrderControlBiz.icOrderBillViewLst(param));
    }

    /**
     * 订单修改
     * @param param
     * @return
     */
    @PostMapping("/info")
    private DefaultVO orderInfo(@RequestBody @Valid Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.orderInfo(idOrder));
    }

    /**
     * 订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search/history")
    private DefaultVO icOrderHistorySearch(@RequestBody @Valid IcListParam param){
        return success(icOrderControlBiz.icOrderSearch(param,true));
    }
    /**
     * 订单详情
     * @param param
     * @return
     */
    @PostMapping("/detail/history")
    private DefaultVO icOrderHistoryDetail(@RequestBody @Valid Map<String,String> param){
        String idOrder;
        if (StrUtil.isEmpty(idOrder=param.get("idOrder")))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.icOrderDetail(idOrder,true));
    }

    /**
     * 订单支付信息
     * @param param
     * @return
     */
    @PostMapping("/billInfo/history")
    private DefaultVO orderHistoryBillInfo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(icOrderControlBiz.getOrderBillInfo(idOrder,true));
    }
}
